const Router = require('koa-router');
const { authenticate,authJWT } = require('@root/config/auth');
const ServerResponse = require('@root/middlewares/resHandler');
const User = require('@root/models/user.model')
const userController = require('@root/controllers/user.controller')
const profileController = require('@root/controllers/profile.controller')

const router = Router();

router.get('/users', authJWT.required, userController.getUser);

router.put('/users', authJWT.required, userController.updateUser);

router.post('/users/login', userController.login);

router.post('/users', userController.register);


router.param('username',async (username,ctx,next)=>{
    const user = await User.findOne({ username });
    
    if (!user) return  ctx.body = ServerResponse.createByErrorMsg('没有找到当前用户');
  
    ctx.state.profile = user;
  
    return next();
})
router.get('/profile/:username',authJWT.required, profileController.getProfile);
router.post('/profile/:username/follow',authJWT.required, profileController.follow);
router.delete('/profile/:username/follow',authJWT.required, profileController.unfollow);

router.get('/profile/:username/followers',authJWT.required,profileController.getFollowers);
module.exports = router.routes();
